package com.cwf.metadatamanage.service.impl;

import com.cwf.metadatamanage.mapper.TableMapper;
import com.cwf.metadatamanage.model.dtos.ColumnDto;
import com.cwf.metadatamanage.model.dtos.TableInfo;
import com.cwf.metadatamanage.model.pojo.Table;
import com.cwf.metadatamanage.service.MetadataShowService;
import com.cwf.metadatamanage.mapper.ColumnMapper;
import com.cwf.metadatamanage.model.vo.req.ColumnReq;
import com.cwf.metadatamanage.model.vo.req.TableReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service("metadataShowService")
public class MetadataShowServiceImpl extends BaseEntityService implements MetadataShowService {

    @Autowired
    private TableMapper tableMapper;

    @Autowired
    private ColumnMapper columnMapper;

    @Override
    public List<TableInfo> getTables(TableReq tableReq) {
        List<TableInfo> tableInfoList = new ArrayList<>();
        List<Table> tableList = this.tableMapper.selectTableList(tableReq.getOrganizationId(), tableReq.getSystemId());
        BeanUtils.copyProperties(tableList, tableInfoList);
        return tableInfoList;
    }

    @Override
    public TableInfo getTableInfoAndColumns(ColumnReq columnReq) {
        TableInfo tableInfo = new TableInfo();

        // 获取Table信息
        Table table = tableMapper.selectById(columnReq.getTableId());
        BeanUtils.copyProperties(table, tableInfo);
        tableInfo.setTableId(table.getId());

        // 获取Table里面的所有Column
        List<ColumnDto> columnDtos = columnMapper.selectByTableId(columnReq.getTableId());
        tableInfo.setColumns(columnDtos);
        return tableInfo;
    }


}
